<template>
  <div class="public-stay-manage app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="name">
        <el-input
          prefix-icon="el-icon-search"
          v-model="queryParams.name"
          placeholder="搜索酒店/民宿名称"
          clearable
          size="small"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" :style="{color: '#3370ff', borderColor: '#3370ff'}" @click="handleQuery">查询</el-button>
        <el-button size="mini" @click="resetQuery">清空</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="end">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="0.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="list" >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true"  />
      <el-table-column label="类型" align="center" prop="type" width="130" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          {{ scope.row.type === 'jiudian' ? '酒店' : scope.row.type === 'minsu' ? '民宿' : scope.row.type || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" width="130" :show-overflow-tooltip="true" />
      <el-table-column label="联系电话" align="center" prop="phone" width="130" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <div class="hpStatusStart" v-if="scope.row.status == '0'">
            <div class="icon">
              <i class="el-icon-check"></i>
            </div>
            {{scope.row.statusName}}
          </div>
          <div class="hpStatusEnd" v-else>
            <div class="icon">
              <i class="el-icon-close"></i>
            </div>
            {{scope.row.statusName}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="roomPage(scope.row)"
          >房间信息</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 'detail')"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 'edit')"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #F54A45"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, deleteFn, exportFn } from "@/api/publicService/stayManage";

export default {
  name: "Logininfor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: {prop: 'loginTime', order: 'descending'},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    getList() {
      this.loading = true;
      list({
        ...this.queryParams
      }).then(response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    roomPage(row) {
      this.$router.push({
        path: '/publicService/stayManage/room',
        query: {
          id: row.id,
        }
      });

    },
    /** 新增 */
    handleAdd() {
      this.$router.push({
        path: '/publicService/stayManage/action',
        query: {
          action: 'add',
        }
      });
    },
    handleUpdate(row, action) {
      this.$router.push({
        path: '/publicService/stayManage/action',
        query: {
          action,
          id: row.id,
          ...this.queryParams
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.name;
      this.$confirm('是否确认删除名称为"' + infoIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteFn(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportFn();
        }).then(res => {
          if (res.code === 200 && res.data) {
            const link = document.createElement("a");
            link.href = res.data;
            link.download = "file-name.ext";
            link.click();
          }
          this.exportLoading = false;
        }).catch(() => {});
    },
  }
};
</script>


<style rel="stylesheet/scss" lang="scss">
.public-stay-manage {
  .hpStatusEnd, .hpStatusStart {
    margin: 0 auto;
    background: #D6E2FF;
    border-radius: 2px 2px 2px 2px;
    padding: 0 10px ;
    line-height: 24px;
    height: 24px;
    font-family: HarmonyOS Sans SC-Regular, HarmonyOS Sans SC;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #245BDB;
    .icon {
      height: 16px;
      width: 16px;
      border-radius: 50%;
      margin-right: 6px;
      background-color: #245BDB;
      line-height: 16px;
      font-size: 12px;
      color: #fff;
    }
  }
  .hpStatusEnd{
    color: #DB6A24;
    background-color: #FFE8D6;
    .icon {
      background-color: #DB6A24;
    }
  }
}
</style>